<?php
/**
 * 任务管理
 */

namespace App\Http\Controllers\Admin;
use App\Common\Utill\Utills;
use App\Http\Controllers\Controller;
use App\Models\Tasks;
use App\Models\WebsitesTopic;
use App\Service\TasksService;
use App\Service\WebsitesService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class TasksController extends Controller
{
    public function index()
    {
        return view('admin.tasks.index');
    }

    /**
     * 列表数据
     * @return void
     */
    public function listData(Request $request)
    {
        $num = $request->input('limit', self::ADMIN_PAGE_NUM);
        $server = new TasksService();
        $result = $server->getListData($request, $num);
        return Utills::resultData($result);
    }

    /**
     * 添加数据展示
     */
    public function createForm()
    {
        $websiteData = WebsitesService::getWebsiteData(1);
        return view('admin.tasks.create')->with('websiteData',$websiteData);
    }

    /**
     * 添加数据保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'website_id'=>'required',
            'websites_topic_id'=>'required',
            'plan_time'=>'required',
            'plan_numbers'=>'required',
        ], [
            'website_id.required' => '网站不能为空',
            'websites_topic_id.required' => '栏目不能为空',
            'plan_time.required' => '时间不能为空',
            'plan_numbers.required' => '数量不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $website_id = $request->input('website_id',"");
        $websites_topic_id = $request->input('websites_topic_id',"");
        $plan_time = $request->input('plan_time',"");
        $plan_numbers = $request->input('plan_numbers',"");
        $server = new TasksService();
        $result = $server->create($website_id,$websites_topic_id,$plan_time,$plan_numbers);
        return Utills::resultData($result);
    }

    /**
     * 编辑展示
     * @return
     */
    public function editForm(Request $request)
    {
        $id = $request->input('id', 0);
        $data = Tasks::where(['id' => $id])->first();
        //网站信息
        $websiteData = WebsitesService::getWebsiteData(1);
        //栏目信息
        $topicName = WebsitesTopic::query()->where('id',$data['websites_topic_id'])->value('titles') ?? '';
        $data['topicName'] = $topicName;
        $data['plan_time_date'] = date('Y-m-d H:i:s',$data["plan_time"]);
        return view('admin.tasks.edit')->with('data', $data)->with('websiteData',$websiteData);
    }

    /**
     * 编辑保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'website_id'=>'required',
            'websites_topic_id'=>'required',
            'plan_time'=>'required',
            'plan_numbers'=>'required',
        ], [
            'website_id.required' => '网站不能为空',
            'websites_topic_id.required' => '栏目不能为空',
            'plan_time.required' => '时间不能为空',
            'plan_numbers.required' => '数量不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id', 0);
        $website_id = $request->input('website_id',"");
        $websites_topic_id = $request->input('websites_topic_id',"");
        $plan_time = $request->input('plan_time',"");
        $plan_numbers = $request->input('plan_numbers',"");
        $server = new TasksService();
        $result = $server->update($id,$website_id,$websites_topic_id,$plan_time,$plan_numbers);
        return Utills::resultData($result);
    }

    /*
    * 删除
    */
    public function delete(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error('删除失败');
        }
        $id = $request->input('id');
        $server = new TasksService();
        $result = $server->delOne($id);
        return Utills::resultData($result);
    }

    /*
     *全部删除
     */
    public function deleteAll(Request $request)
    {
        $idArr = $request->input('idList',"");
        $server = new TasksService();
        $result = $server->delAll($idArr);
        return Utills::resultData($result);
    }



}
